#include "StdAfx.h"
#include "DictManager.h"


CDictManager::CDictManager(void)
{
}


CDictManager::~CDictManager(void)
{
}
int CDictManager::Add(int iClassID,CString strKey,CString strContent,CString strSample)
{
	//CString strSQL = _T("inset into km_category(parent,category,classtype)values(?,?,?)");
	static LPCWSTR strSQL = L"insert into km_Dict(ClassID,Key,Content,Sample)values(?,?,?,?);";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(strSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,iClassID);
	pSQLStatement->Bind(2,strKey);
	pSQLStatement->Bind(3,strContent);
	pSQLStatement->Bind(4,strSample);
	BOOL Res = SUCCEEDED(pSQLStatement->ExecuteUpdate());

	if(Res)
	{
		m_pDB->CommitTransaction();
		return 12;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return 13;
	}
}
int CDictManager::Update(int iID,CString strContent,CString strSample)
{
	static LPCWSTR strSQL = L"update km_Dict set Content=?,Sample =? where id=?;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(strSQL,&pSQLStatement);

	pSQLStatement->Bind(1,strContent);
	pSQLStatement->Bind(2,strSample);
	pSQLStatement->Bind(3,iID);
	
	int ret = pSQLStatement->ExecuteScalar();

	if(ret > 0)
	{
		m_pDB->CommitTransaction();
		return 10;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return 11;
	}
}
int CDictManager::Delete(int iID)
{
	static LPCWSTR szSQL = L"delete from km_dict where id=?;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->BeginTransaction();
	m_pDB->PrepareStatement(szSQL,&pSQLStatement);

	pSQLStatement->Bind(1,iID);
	
	int ret = pSQLStatement->ExecuteScalar();

	if(ret > 0)
	{
		m_pDB->CommitTransaction();
		return 29;
	}
	else
	{
		m_pDB->RollbackTransaction();
		return 30;
	}
}


void  CDictManager::GetKeyList(int iClassID, CStringList& liKeys)
{
	static LPCWSTR szSQL = L"select key from km_dict where classid=? order by key;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->PrepareStatement(szSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,iClassID);

	CComPtr<Skylark::ISQLiteComResultSet3> pRs;
	HRESULT hr = pSQLStatement->ExecuteQuery(&pRs);
	if(!SUCCEEDED(hr) || pRs->IsEof())
		return ;
	while(!pRs->IsEof())
	{
	
		liKeys.Add(CString(pRs->GetAsString(L"key")));
		pRs->NextRow();
	}
}
void CDictManager::GetDictByID(int iID,CString& strKey,CString& strContent,CString& strSample)
{
	static LPCWSTR szSQL = L"select key,Content,Sample from km_dict where id=? ;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->PrepareStatement(szSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,iID);

	CComPtr<Skylark::ISQLiteComResultSet3> pRs;
	HRESULT hr = pSQLStatement->ExecuteQuery(&pRs);
	if(!SUCCEEDED(hr) || pRs->IsEof())
		return ;
	if(!pRs->IsEof())
	{
		strKey = pRs->GetAsString(L"key");
		strContent = pRs->GetAsString(L"content");
		strSample = pRs->GetAsString(L"sample");
		//query.NextRecord();
	}
}
void CDictManager::GetDict(int iClassID ,CString strKey,CString& strContent,CString& strSample)
{
	static LPCWSTR szSQL = L"select Content,Sample from km_dict where classid=? and Key=? ;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->PrepareStatement(szSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,iClassID);
	pSQLStatement->Bind(2,strKey);

	CComPtr<Skylark::ISQLiteComResultSet3> pRs;
	HRESULT hr = pSQLStatement->ExecuteQuery(&pRs);
	if(!SUCCEEDED(hr) || pRs->IsEof())
		return ;
	if(!pRs->IsEof())
	{
		//strKey = CString(query.GetStringField("key"));
		strContent = pRs->GetAsString(L"content");
		strSample = pRs->GetAsString(L"sample");
		//query.NextRecord();
	}
}

int CDictManager::Save(int iClassID,CString strKey,CString strContent,CString strSample)
{
	static LPCWSTR szSQL = L"select id from km_dict where classid=? and key=? ;";
	CComPtr<ISQLiteComStatement3> pSQLStatement;
	m_pDB->PrepareStatement(szSQL,&pSQLStatement);
	//CUnicodeToMultiByte mb(strCategory);
	pSQLStatement->Bind(1,iClassID);
	pSQLStatement->Bind(2,strKey);
	int iId = 0;
	CComPtr<Skylark::ISQLiteComResultSet3> pRs;
	HRESULT hr = pSQLStatement->ExecuteQuery(&pRs);
	if(!SUCCEEDED(hr) )
		return 0;
	if(!pRs->IsEof())
	{
		iId = pRs->GetInt(L"id");
		return Update(iId,strContent,strSample);
	}
	else
	{
		return Add(iClassID,strKey,strContent,strSample);
	}
}